package com.milorad.ads.pages;

import java.util.List;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.milorad.ads.data.dao.interfaces.AdvertDao;
import com.milorad.ads.data.dao.interfaces.CategoryDao;
import com.milorad.ads.data.dao.interfaces.CityDao;
import com.milorad.ads.data.domain.Advert;
import com.milorad.ads.data.domain.Advert.AdvertStatus;
import com.milorad.ads.data.domain.Category;
import com.milorad.ads.data.domain.City;

public class AdvertsFilterPage extends BasePage {
	
	@SpringBean
	private CategoryDao categoryDao;	
	@SpringBean
	private AdvertDao advertDao;
	@SpringBean
	private CityDao cityDao;
	
	public AdvertsFilterPage() {			
		
		addFilter();		
		addCategoriesMenu();
	}

	/**
	 * Adds adverts filter to the page.
	 */
	private void addFilter() {
		
		Form<Advert> form = new Form<Advert>("filterAdvertsForm", new CompoundPropertyModel<Advert>(new Advert())) {
			private static final long serialVersionUID = -4344919260680439226L;
			
			@Override
			protected void onSubmit() {																		
				// filter button is clicked
				
				Advert filterAdvert = getModelObject();
				filterAdvert.setStatus(AdvertStatus.APPROVED);
				List<Advert> adverts = advertDao.find(filterAdvert);								
				setResponsePage(new AdvertsOverviewPage(adverts));
			}			
		};
		
		FormComponent<Category> categoryDropDown = new DropDownChoice<Category>("category", categoryDao.findAll());
		form.add(categoryDropDown);
		FormComponent<City> cityDropDown = new DropDownChoice<City>("city", cityDao.findAll());
		form.add(cityDropDown);
		form.add(new TextArea<Advert>("text"));		
		add(form);
	}

	/**
	 * Adds categories menu to the page.
	 */
	private void addCategoriesMenu() {
		
		List<Category> categories = categoryDao.findAll();
		
		addCategoriesTitle(categories);
		
		addCategoriesList(categories);
	}

	private void addCategoriesList(List<Category> categories) {
		
		add(new ListView<Category>("categoriesList", categories) {

			private static final long serialVersionUID = -4083520522187567842L;
			

			@Override
			protected void populateItem(ListItem<Category> item) {
				Category category = item.getModelObject();						
						
				Link<Category> link = new Link<Category>("showCategory", item.getModel()) {
					
					private static final long serialVersionUID = 2221207599260261693L;
					
					@Override
					public void onClick() {
						// user chose category from categories menu
						
						Category filterCategory = getModelObject();
						
						Advert advert = new Advert();
						advert.setCategory(filterCategory);
						advert.setStatus(AdvertStatus.APPROVED);
						List<Advert> filteredAdverts = advertDao.find(advert);
						
						setResponsePage(new AdvertsOverviewPage(filteredAdverts));						
					}					
				};
				
				item.add(link);
				
				link.add(new Label("categoryName", category.getName()));
			}						
		});
	}

	private void addCategoriesTitle(List<Category> categories) {
		
		if (categories.isEmpty()) {
			add(new Label("categoriesTitle", "(no categories currently)"));
		} else {
			add(new Label("categoriesTitle", "Categories:"));
		}
	}

}